package sfsoftware.game.trevor.ui;

import java.awt.Font;
import java.util.Vector;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;

import sfsoftware.game.trevor.Player;
import sfsoftware.util.Sound;
import sfsoftware.util.graphics.Graphics;
import sfsoftware.util.graphics.RGBA;
import sfsoftware.util.input.Gamepad;
import sfsoftware.util.input.Kboard;

public class Dialog {
	public static boolean displayDialog = false;
	private static boolean cursorMoveIn = false;
	private static int cursorX = 161;
	public static int result = 0;
	private static int textCounter = 0;
	private static int rows = 0;
	private static String currentText = "";
	private static TrueTypeFont font;
	private static String[] dialogOptions = new String[0];
	
	
	static
	{
		Font awtFont = new Font("Verdana", Font.BOLD, 12);
		font = new TrueTypeFont(awtFont, false);		
	}
	
	public Dialog() {
		// TODO Auto-generated constructor stub
	}
	
	public static void closeDialog()
	{
		if(textCounter == currentText.length())
		{
			if(dialogOptions != null && dialogOptions.length > 0)
			{
				// If they selected a menu item.
				Sound.playSound("menu_go");
			}
			currentText = "";
			displayDialog = false;
			dialogOptions = new String[0];
		}
	}
	
	public static void pollDialogInput()
	{
		if(dialogOptions.length > 0 && textCounter == currentText.length())
		{			
			if(Kboard.keyPressed(Kboard.downKeys) || Gamepad.povYDownPressed() || Gamepad.yAxisDownPressed())
			{
				Sound.playSound("menu_sel");
				result++;
				if(result >= dialogOptions.length)
				{
					result = 0;					
				}
			}
		
			if(Kboard.keyPressed(Kboard.upKeys) || Gamepad.povYUpPressed() || Gamepad.yAxisUpPressed())
			{
				Sound.playSound("menu_sel");
				result--;
				if(result < 0)
				{
					result = dialogOptions.length - 1;					
				}							
			}
		}
		
		if(Kboard.keyPressed(Keyboard.KEY_SPACE) || Gamepad.buttonPressed(Gamepad.BUTTON_A))
		{
			Dialog.closeDialog();			
		}		
	}
	
	public static void raiseDialog(String text)
	{
		result = 0;
		textCounter = 0;
		currentText = text.trim();		
		rows = (int) Math.ceil(font.getWidth(currentText) / 300.0);
		
		displayDialog = true;		
	}	
	
	public static void setChoices(String[] choices)
	{
		dialogOptions = choices;		
	}
	
	public static void setChoices(Vector<String> choices)
	{
		String[] s = new String[choices.size()];
		choices.toArray(s);
		setChoices(s);
	}
	
	public static void updateDialog()
	{
		if(displayDialog)
		{		
			int dialogOffsetY = -40;
			int dialogHeight = 80;											
			dialogHeight = 15 + (rows * font.getHeight()) + (dialogOptions.length * 25);					
			
			if(Player.getYPos() < 150)
			{
				dialogOffsetY = 332 - dialogHeight;							
			}
									
			Graphics.drawRect(139, dialogOffsetY + 57, 362, dialogHeight + 2, 0, 0, 0, 0.85f, 1);
			Graphics.drawRect(140, dialogOffsetY + 58, 360, dialogHeight, 1, 1, 1, 1, 4);						
			Graphics.drawRect(143, dialogOffsetY + 61, 354, dialogHeight - 6, 0, 0, 0, 0.75f, 1);
			
			RGBA blue = new RGBA(0.1f, 0.2f, 0.3f, 0.75f); //new RGBA(0, 0, 0.7f, 0.75f);
			RGBA darkBlue = new RGBA(0, 0, 0.1f, 0.95f);
			Graphics.drawQuad(144, dialogOffsetY + 62, 352, dialogHeight - 8, blue, blue, darkBlue, darkBlue);			
			
			int textY = 65;
			int xCounter = 0;
			int lineStart = 0;
			for(int i = 0; i < textCounter; i++)
			{			
				// If we hit a space, determine if we should start a new line
				if(currentText.charAt(i) == ' ')
				{		
					// If the current line length in addition to the next word will put us past 300, start a new line.
					if(currentText.indexOf(' ', i + 1) != -1 && (font.getWidth(currentText.substring(lineStart, i)) + font.getWidth(currentText.substring(i, currentText.indexOf(' ', i + 1)))) > 300) 
					{
						//System.out.println(currentText.substring(i, currentText.indexOf(' ', i + 1)));
						textY = textY + 15;
						xCounter = 0;					
						i++;
						lineStart = i;
					}
				}
				
				String currentChar = String.valueOf(currentText.charAt(i));	
				int charWidth = font.getWidth(currentText.substring(lineStart, i));								
				
				font.drawString(155 + charWidth, dialogOffsetY + textY, currentChar, Color.white);
				xCounter = xCounter + 1;
			}			
				
			if(textCounter < currentText.length())
			{
				Sound.playSound("dialog");
				textCounter++;				
			}
			else 
			{
				for(int i = 0; i < dialogOptions.length; i++)
				{
					if(result == i)
					{
						Graphics.drawSprite(cursorX, (dialogOffsetY + 65) + (rows * 18) + (i * 15) + 2, "cursor", 20, 15, 0, 0, 20, 15, 1, 1, 1, 1);
						font.drawString(185, (dialogOffsetY + 65) + (rows * 18) + (i * 15), dialogOptions[i], Color.red);
					}
					else 
					{
						font.drawString(185 , (dialogOffsetY + 65) + (rows * 18) + (i * 15), dialogOptions[i], Color.white);
					}
				}
			}
			
			if(cursorMoveIn)
			{
				cursorX = cursorX - 1;
				if(cursorX <= 151){
					cursorX = 151;
					cursorMoveIn = false;
				}			
			}
			else
			{
				cursorX = cursorX + 2;
				if(cursorX >= 161){
					cursorX = 161;
					cursorMoveIn = true;
				}
			}
			
		}	
	}

}
